package com.tomdxz.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;

import com.tomdxz.dao.TreeDao;
import com.tomdxz.util.JDBCutil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class TreeDaoImpl implements TreeDao {

	@Override
	public JSONArray getTreeByPid(int pId,String fId) {
		JSONArray js = new JSONArray();
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT fid,fname,fstate,furl FROM c_function");
		sql.append(" WHERE pid=? and fid IN (" + fId + ")");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1,pId );
			ResultSet rs = pst.executeQuery();
			while (rs.next()) {
				JSONObject j = new JSONObject();
				j.put("id", rs.getInt("fid"));
				j.put("text", rs.getString("fname"));
				j.put("state", rs.getString("fstate"));
				JSONObject attr = new JSONObject();
				attr.put("url", rs.getString("furl"));
				j.put("attributes", attr);
				js.add(j);
			}
			JDBCutil.closeJDBC(pst, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return js;
	}

	@Override
	public JSONArray getAllTreeByPid(int pId,String fId) {
		JSONArray js = this.getTreeByPid(pId,fId);
		for (int i = 0; i < js.size(); i++) {
			String state = js.getJSONObject(i).getString("state");
			if ("closed".equals(state)) {
				int id = js.getJSONObject(i).getInt("id");
				JSONArray children = this.getAllTreeByPid(id,fId);
				js.getJSONObject(i).put("children", children);
			}
		}
		return js;
	}

	@Override
	public String getFidByUid(int uId) {
		String pId = null;
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT c_role.pid FROM c_role");
		sql.append(" INNER JOIN c_user");
		sql.append(" ON c_user.rid = c_role.rid");
		sql.append(" WHERE c_user.uid = ?");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, uId);
			ResultSet rs = pst.executeQuery();
			if (rs.next()) {
				pId = rs.getString("pid");
			}
			JDBCutil.closeJDBC(pst, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return pId;
	}

	@Override
	public JSONArray getWholeTreeByPid(int pId,String fId) {
		JSONArray js = new JSONArray();
		String[] fids = fId.split(",");
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT fid,fname,fstate,furl FROM c_function");
		sql.append(" WHERE pid=? ");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1,pId );
			ResultSet rs = pst.executeQuery();
			while (rs.next()) {
				JSONObject j = new JSONObject();
				j.put("id", rs.getInt("fid"));
				j.put("text", rs.getString("fname"));
				j.put("state", rs.getString("fstate"));
				for(int i=0;i<fids.length;i++){
					if((Integer.parseInt(fids[i])==(rs.getInt("fid")))&&(rs.getString("fstate").equals("open"))){
						j.put("checked", true);
					}
				}				
				JSONObject attr = new JSONObject();
				attr.put("url", rs.getString("furl"));
				j.put("attributes", attr);
				js.add(j);
			}
			JDBCutil.closeJDBC(pst, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return js;
	}
	

	@Override
	public JSONArray getWholeAllTreeByPid(int pId,String fId) {
		JSONArray js = this.getWholeTreeByPid(pId,fId);
		for (int i = 0; i < js.size(); i++) {
			String state = js.getJSONObject(i).getString("state");
			if ("closed".equals(state)) {
				int id = js.getJSONObject(i).getInt("id");
				JSONArray children = this.getWholeAllTreeByPid(id,fId);
				js.getJSONObject(i).put("children", children);
			}
		}
		return js;
	}

	@Override
	public String getFidByRid(int rId) {
		String pId = null;
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT c_role.pid FROM c_role");
		sql.append(" WHERE c_role.rid = ?");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1, rId);
			ResultSet rs = pst.executeQuery();
			if (rs.next()) {
				pId = rs.getString("pid");
			}
			JDBCutil.closeJDBC(pst, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return pId;
	}

	@Override
	public JSONArray getOwnTreeByPid(int pId) {
		JSONArray js = new JSONArray();
		StringBuffer sql = new StringBuffer();
		sql.append("SELECT fid,fname,fstate,furl FROM c_function");
		sql.append(" WHERE pid=?");
		Connection con = JDBCutil.getConnection();
		try {
			PreparedStatement pst = con.prepareStatement(sql.toString());
			pst.setInt(1,pId );
			ResultSet rs = pst.executeQuery();
			while (rs.next()) {
				JSONObject j = new JSONObject();
				j.put("id", rs.getInt("fid"));
				j.put("text", rs.getString("fname"));
				j.put("state", rs.getString("fstate"));
				JSONObject attr = new JSONObject();
				attr.put("url", rs.getString("furl"));
				j.put("attributes", attr);
				js.add(j);
			}
			JDBCutil.closeJDBC(pst, con);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return js;
	}

	@Override
	public JSONArray getAllOwnTreeByPid(int pId) {
		JSONArray js = this.getOwnTreeByPid(pId);
		for (int i = 0; i < js.size(); i++) {
			String state = js.getJSONObject(i).getString("state");
			if ("closed".equals(state)) {
				int id = js.getJSONObject(i).getInt("id");
				JSONArray children = this.getAllOwnTreeByPid(id);
				js.getJSONObject(i).put("children", children);
			}
		}
		return js;
	}

}
